<?php

/*
 * EachGo电商网站 新媒体
 * www.eachgo.cn
 */
namespace Home\Controller;
use Think\Controller;
class OrderInfoController extends Controller{
    public function addOrderInfo()
    {
        //var_dump($_POST);
        $userInfo=session('UserInfo');
        $data=array_merge($_POST,array('user_phoneNumber'=>$userInfo['phonenumber']));
        //var_dump($data);
        if(!empty($_POST)){
            if($data['address']!=NULL && $data['zipCode']!=NULL && $data['consignee']!=NULL && $data['contactPhone']!=NULL ) {
                echo "进了";
                $address = D("shippingAddress");
                $newAddressId = $address->addAddress($data);//新的地址的id
            }else{
                $newAddressId=$data['optionsRadios'];
            }
        }
        $newData=array(
            'user_ID'=>$userInfo['id'],
            'shippingAddress_ID'=>$newAddressId,
            'isBingo'=>1,
            'orderDate'=>date('Y-m-d h:i:s'),
            'payDate'=>date('Y-m-d h:i:s'),
        );
        $newOrderInfo=array_merge($newData,$data);
        //var_dump($newOrderInfo);
        $order = D('OrderInfo');
        $newOrderId=$order->addOrder($newOrderInfo);
        $detailOrder=array(
            'orderInfo_ID'=>$newOrderId,
            'orderNumber'=>1,
        );
        $newDetailOrder=array_merge($detailOrder,$newOrderInfo);
        //var_dump($newDetailOrder);
        $detailedOrder=D('OrderDetailedInfo');
        $result=$detailedOrder->addDetaileOrder($newDetailOrder);
        $UserLotter=D('UserLotteryInfo');
        var_dump($newDetailOrder);
        $info=$UserLotter->deleteByID($newDetailOrder['userLotterId']);
        //echo ">>>";
        var_dump($info);
        $this->redirect('OrderInfo/order',array(),5,'');
    }
    public function delOrder()//删除订单信息，在需要删除的订单的id,$_GET['id']
    {
//        var_dump($_GET);
        if(!empty($_GET)){
            $order = D('OrderInfo');
            $result = $order->deleteOrder($_GET['id']);
            var_dump($result);
        }else{
            echo "GET为空";
        }
        $this->redirect('OrderInfo/order',array(),0,'');
    }

    public function addressManage($userInfo){
        $address=D("shippingAddress");
        $AddressInfo=$address->queryAllAddress($userInfo['phonenumber']);
        return $AddressInfo;
    }
    public function bingo(){
        $userInfo=session('UserInfo');
        if(empty($_POST)&&empty($_GET)){
            session('search',' ');
            session('type',0);
            session('time',1000);
        }
        else if (!empty($_POST)) {//如果搜索框有内容，则提取内容
            if($_POST['search']==NULL){
                session('search',' ');//如果内容为空，则设置未空格字符串
            }else {
                session('search', $_POST['search']);//如果内容不为空，则设置搜索框中的内容
            }
        } else if (!empty($_GET)) {//时间选择
            if($_GET['time']==NULL && session('time')==NULL){
                session('time',1000);
            }else if($_GET['time']!=NULL){
                session('time',$_GET['time']);//time值赋值为新的get内的值
            }
            if($_GET['type']==NULL && session('type')==NULL){
                session('type',0);
            }else if($_GET['type']!=NULL){//如果重新选择拼购类型，则把时间筛选重置为全部
                session('type',$_GET['type']);
                session('time',1000);
            }
            session('search',' ');
        }
        $order=D('UserLotteryInfo');
        $bingoInfo=$order->queryAllBingo($userInfo['id']);//获取所有的拼购
        //var_dump($bingoInfo);
        $turn=count($bingoInfo);
        $bingoType=A('OrderInfo');
        if(session('type')==1){//判断拼购类型，1表示正在继续的拼购
            $bingoInfo=$bingoType->findIncomplete($bingoInfo,$turn);
        }else if(session('type')==2){//2表示已揭晓的拼购
            $bingoInfo=$bingoType->findCompleted($bingoInfo,$turn);
        }
        if(session('search')!=' ') {//如果session中search
            $bingoInfo = $bingoType->orderSearch($bingoInfo, session('search'),$turn);
        }
        //时间筛选
        $bingoInfo=$bingoType->bingoTimeFilter($bingoInfo,session('time'),$turn);
        $AddressInfo=$bingoType->addressManage($userInfo);
        $bingoInfo=$bingoType->isShared($bingoInfo,$userInfo['id']);
            //var_dump($bingoInfo);
        $this->assign('AddressInfo',$AddressInfo);
        $this->assign('bingoInfo',$bingoInfo);
        $this->display();
    }
    public function order()
    {

        //var_dump($_POST);
        $order = D('OrderDetailedInfo');
        $userInfo=session('UserInfo');
        if(empty($_POST)&&empty($_GET)){
            session('search',' ');
            session('type',0);
            session('time',1000);
            session('status',0);
        }
        else if (!empty($_POST)) {//如果搜索框有内容，则提取内容

            if($_POST['search']==NULL){
                session('search',' ');//如果内容为空，则设置未空格字符串
            }else {
                session('search', $_POST['search']);//如果内容不为空，则设置搜索框中的内容
            }
        } else if (!empty($_GET)) {//时间选择

            if ($_GET['time'] == NULL && session('time') == NULL) {
                session('time', 100);
            } else if ($_GET['time'] != NULL) {
                session('time', $_GET['time']);//time值赋值为新的get内的值
            }
            if ($_GET['status'] == NULL && session('status') == NULL) {
                session('status', 0);
            } else if ($_GET['status'] != NULL) {//如果重新选择拼购类型，则把时间筛选重置为全部
                session('status', $_GET['status']);
                session('time', 100);
            }
            if ($_GET['type'] == NULL && session('type') == NULL) {
                session('type', 0);
            } else if ($_GET['type'] != NULL) {
                session('type', $_GET['type']);
                session('status', 0);
                session('time', 100);
            }
            session('search', ' ');
        }
        $orderController=A('OrderInfo');
        $orderInfo=$order->queryOrder($userInfo['id']);
        $turn=count($orderInfo);
        $orderInfo=$orderController->addIsevaluated($orderInfo,$turn);
        $orderInfo=$orderController->find($orderInfo,session('type'),$turn);
        $orderInfo=$orderController->findByType($orderInfo,$turn,session('status'),session('type'));
        if(session('search')!=' ') {//如果session中search
            //var_dump(session('search'));
            $orderInfo = $orderController->orderSearch($orderInfo, session('search'),$turn);
        }

        $orderInfo=$orderController->orderTimeFilter($orderInfo,session('time'),$turn);
        //var_dump($orderInfo);
        $orderInfo=$orderController->isReturn($orderInfo,$turn);
        $this->assign('orderInfo',$orderInfo);
        $this->display("OrderInfo/order");
    }
    public function findOneOrder(){
        //var_dump($_GET);
        $order = D('OrderDetailedInfo');
        $orderInfo=$order->queryOneOrder($_GET['orderId']);
        $this->assign('orderInfo',$orderInfo);
        $this->display("OrderInfo/order");
    }
    public function singleInfo(){
        //var_dump($_GET);
        $orderId=$_GET['orderId'];
        $order=D('OrderInfo');
        if(!empty($_GET)){
            $singleInfo=$order->findSinleInfo($orderId);
            $singleInfo=array_merge($singleInfo[0],$_GET);
            $this->assign('singleInfo',$singleInfo);
            $this->display();
        }else {
            $this->display();
        }
    }
    public function findByType($orderInfo,$turn,$status,$type){
    //根据状态查找，拼购是否完成，订单的几种状态

        if($type==1){
            if($status==1){
                $orderInfo=$this->findIncomplete($orderInfo,$turn);
            }else if($status==2){
                $orderInfo=$this->findCompleted($orderInfo,$turn);
            }

        }else if($type==2){
            for($i=0;$i<$turn;$i++){
                $temp=$orderInfo[$i];
                if($status==1){
                    if($temp['paydate']>'1971-01-01'){
                        unset($orderInfo[$i]);
                    }
                }else if($status==2){
                    if($temp['deliverydate']>'1971-01-01' ||$temp['paydate']<'1971-01-01'){
                        unset($orderInfo[$i]);
                    }
                }else if($status==3){
                    if($temp['confirmreceiptdate']>'1971-01-01' || $temp['deliverydate']<'1971-01-01' ||$temp['paydate']<'1971-01-01'){
                        unset($orderInfo[$i]);
                    }
                }else if($status==4){
                    $order = D('OrderDetailedInfo');
                    $result=$order->queryIdEvaluated(session('UserInfo.id'));//查询出没有评价的订单ID
                    //var_dump($temp);
                    $turn2=count($result);
                    //var_dump($turn);
                    for($j=0;$j<$turn2;$j++){
                        $flag=0;
                        $info=$result[$j];
                        //echo "whey?";
                        //var_dump($orderInfo);
                        if($info['evaluated']==$temp['orderid'] && $temp['confirmreceiptdate']>'1970-01-01'){//如果存在于结果集里面，则表示为没有评价的订单
                            $orderInfo[$i]=array_merge($orderInfo[$i],array('isevaluated',1));
                            $flag=1;
                        }
                        if($flag==0){//flag值为0，表示不存在结果集里面，则删除
                            unset($orderInfo[$i]);
                        }
                    }

                }
            }
        }
        return $orderInfo;
    }
    //判断是否已经评价，并添加isevaluated区别，1表示已评价，0表示未评价,2表示未完成的订单
    public function addIsevaluated($orderInfo,$trun){
        $order = D('OrderDetailedInfo');
        $result=$order->queryIdEvaluated(session('UserInfo.id'));//查询出没有评价的订单ID
        //var_dump($temp);
        $turn2=count($result);
        //var_dump($turn);
        for($i=0;$i<$trun;$i++) {
            $temp=$orderInfo[$i];
            //var_dump($temp);
            for ($j = 0; $j < $turn2; $j++) {
                $flag = 0;
                $info = $result[$j];
                //echo "whey?";
                //var_dump($orderInfo);
                if ($temp['confirmreceiptdate'] > '1970-01-01') {
                    $orderInfo[$i] = array_merge($orderInfo[$i], array('isevaluated'=>0));

                }else if ($info['evaluated'] == $temp['orderid'] && $temp['confirmreceiptdate'] > '1970-01-01') {//如果存在于结果集里面，则表示为没有评价的订单
                    $orderInfo[$i] = array_merge($orderInfo[$i], array('isevaluated'=> 1));
                }else {
                    $orderInfo[$i] = array_merge($orderInfo[$i], array('isevaluated'=>2));
                }
            }
        }
        return $orderInfo;
    }
    public function find($orderInfo,$type,$turn){//区别是拼购还是购物

        if($type==1) {
            for ($i = 0; $i <= $turn; $i++) {
                $temp = $orderInfo[$i];
                if ($temp['isbingo'] == 0) {
                    unset($orderInfo[$i]);
                }
            }
        }else if($type==2){
            for($i = 0; $i <= $turn; $i++){
                $temp=$orderInfo[$i];
                if($temp['isbingo']==1){
                    unset($orderInfo[$i]);
                }
            }
        }
//        var_dump($orderInfo);
        return $orderInfo;
    }
//    public function orderFilter($orderInfo, $type, $time)
//    {
//        //$time如果是N个月则为整数N，如果是全部，则为100
//        if ($orderInfo[0]['isbingo']) {
//            for ($i = 0; $i < count($orderInfo); $i++) {
//                $info = $orderInfo[$i];
//                if ($type == 2) {
//                    if ($info['orderdate'] < date('Y-m-d h:i:s', time() - $time * 30 * 86400)) {
//                        unset($orderInfo[$i]);
//                    }
//                } else {
//                    //var_dump($info);
//                    if ($info['confirmreceiptdate'] < date('Y-m-d h:i:s', time() - $time * 30 * 86400)) {
//                        unset($orderInfo[$i]);
//                    }
//                }
//            }
//        } else {
//            for ($i = 0; $i < count($orderInfo); $i++) {
//                $info = $orderInfo[$i];
//                if ($type == 0 OR $type == 1) {//全部和未付款
//                    if ($info['orderdate'] < date('Y-m-d h:i:s', time() - $time * 30 * 86400)) {
//                        unset($orderInfo[$i]);
//                    }
//                } else if ($type == 2) {//待发货
//                    //var_dump($info);
//                    if ($info['payDate'] < date('Y-m-d h:i:s', time() - $time * 30 * 86400)) {
//                        unset($orderInfo[$i]);
//                    }
//                }else if ($type == 3){//待收货
//                        if ($info['confirmReceiptDate'] < date('Y-m-d h:i:s', time() - $time * 30 * 86400)) {
//                            unset($orderInfo[$i]);
//                        }
//                }
//                return $orderInfo;
//            }
//        }
//    }
    public function orderTimeFilter($orderInfo, $time,$turn)//订单时间筛选
    {
        //$time如果是N个月则为整数N，如果是全部，则为100
        for ($i = 0; $i <= $turn; $i++) {
            $info = $orderInfo[$i];
            $times=date('Y-m-d h:i:s', time() - $time * 30 * 86400);
            if ($info['orderdate'] <$times ) {
                unset($orderInfo[$i]);
            }
        }
        return $orderInfo;
    }
    public function bingoTimeFilter($orderInfo, $time,$turn)//我的拼购时间筛选
    {
        //$time如果是N个月则为整数N，如果是全部，则为100
        for ($i = 0; $i <= $turn; $i++) {
            $info = $orderInfo[$i];
            $times=date('Y-m-d h:i:s', time() - $time * 30 * 86400);
            if ($info['joindate'] <$times ) {
                unset($orderInfo[$i]);
            }
        }
        return $orderInfo;
    }
    public function orderSearch($orderInfo,$search,$turn){//订单搜索方法，实现按照商品名字搜索
        for ($i = 0; $i <=$turn; $i++) {
            $info = $orderInfo[$i];
            if (!strstr($info['goodsname'],$search)) {
                unset($orderInfo[$i]);
            }
        }
        return $orderInfo;
    }
    public function findIncomplete($bingoInfo,$turn){//未揭晓的拼购
        for($i=0;$i<=$turn;$i++){
            $info=$bingoInfo[$i];
            if($info['number']==$info['total']){//如果存在已经揭晓的拼购，则删除
                unset($bingoInfo[$i]);
            }
        }
        return $bingoInfo;
    }
    public function findCompleted($bingoInfo,$turn){//揭晓了的拼购
        for($i=0;$i<=$turn;$i++){
            $info=$bingoInfo[$i];
            if((int)$info['number']<(int)$info['total']){//如果存在未揭晓的拼购，则删除
                unset($bingoInfo[$i]);
            }
        }
        return $bingoInfo;
    }
    public function isReturn($orderInfo,$turn){//退货订单
        $return=D("ReturnInfo");
        $userInfo=session('UserInfo');
        $returnIds=$return->getIdReturn($userInfo['id']);
        $number=count($returnIds);//获取返回的数组的数量
//        var_dump($turn);
        for($i=0;$i<$turn;$i++){
            $orderTemp=$orderInfo[$i];
            //var_dump($orderTemp);
            //echo "what?";
            for($j=0;$j<$number;$j++){
                $returnTemp=$returnIds[$j];
//                echo "what?";
//                var_dump($orderTemp['detailid']);
//                var_dump($returnTemp['id']);
                if($orderTemp['detailid']==$returnTemp['id']){
                    $orderInfo[$i]=array_merge($orderInfo[$i],array('isReturn'=>1));
                }else{
                    $orderInfo[$i]=array_merge($orderInfo[$i],array('isReturn'=>0));
                }
            }
            if(empty($orderInfo[$i])){
                unset($orderInfo[$i]);
            }
        }
        //var_dump($returnIds);
        return $orderInfo;
    }
    public function isShared($bingoInfo,$userId){
        $turn=count($bingoInfo);
        $share=D('Share');
        $number=$share->numberShared($userId);
        //var_dump($number);
        //var_dump($bingoInfo);
        $turn2=count($number);
        for($i=0;$i<$turn;$i++){
            $tempBingo=$bingoInfo[$i];
            //var_dump($tempBingo);
            for($j=0;$j<$turn2;$j++){
                $tempNumber=$number[$j];
                //0表示未中奖，1表示未分享，2表示已分享
                if($tempBingo['userlotterid']==$tempNumber['userlotterid']){
                    $bingoInfo[$i]=array_merge($bingoInfo[$i],array('isShare'=>2));
                }else if($tempBingo['iswin']==1){
                    $bingoInfo[$i]=array_merge($bingoInfo[$i],array('isShare'=>1));
                }else{
                    $bingoInfo[$i]=array_merge($bingoInfo[$i],array('isShare'=>0));
                }
            }
            if(empty($bingoInfo[$i])){
                unset($bingoInfo[$i]);
            }
        }
        return $bingoInfo;
    }
}
